package org.lxy.msg;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.lxy.core.mq.AbstractRocketConsumer;
import org.lxy.domain.PointsLogEntity;
import org.lxy.domain.RecordLogEntity;
import org.lxy.domain.RecycleGoodsEntity;
import org.lxy.domain.UserInfoEntity;
import org.lxy.domain.constant.RocketTag;
import org.lxy.domain.constant.RocketTopic;
import org.lxy.domain.mq.ChangeReq;
import org.lxy.service.PointsLogService;
import org.lxy.service.RecordLogService;
import org.lxy.service.RecycleGoodsService;
import org.lxy.utils.CoreUtils;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @author 卢心怡
 */
@Component
@Slf4j
public class DefaultMqListener extends AbstractRocketConsumer {
    @Override
    public void init() {
        registerMessageListener(RocketTopic.RECYCLING_GOODS_COMMON.getName(), RocketTag.ALL, RocketTopic.RECYCLING_GOODS_COMMON.getDesc(), (MessageListenerOrderly) (m, c) -> {
            m.forEach(e -> {
                try {
                    log.info("topic:[{}] tag:[{}]  监听到消息:{}", e.getTopic(), e.getTags(), new String(e.getBody(), RemotingHelper.DEFAULT_CHARSET));
                } catch (UnsupportedEncodingException unsupportedEncodingException) {
                    log.error("解析mq消息体异常  param[ topic:{},tag:{} ]  ", e.getTopic(), e.getTags(), unsupportedEncodingException);
                }
                if (RocketTag.SAVE_POINTSLOG.equals(e.getTags())) {
                    ChangeReq changeReq = JSONObject.parseObject(e.getBody(), ChangeReq.class);
                    PointsLogService pointsLogService = CoreUtils.pointsLogService;
                    RecycleGoodsService recycleGoodsService = CoreUtils.recycleGoodsService;
                    RecycleGoodsEntity entity = recycleGoodsService.getById(changeReq.getGoodsId());
                    UserInfoEntity userInfoEntity = CoreUtils.userInfoService.getById(changeReq.getUserId());
                    PointsLogEntity jifen = new PointsLogEntity();
                    jifen.setPoints(entity.getChangePoints());
                    jifen.setUserId(changeReq.getUserId());
                    jifen.setGoodsId(entity.getId());
                    pointsLogService.save(jifen);
                    userInfoEntity.setPoints(userInfoEntity.getPoints()-entity.getChangePoints());
                    CoreUtils.userInfoService.updateById(userInfoEntity);
                    log.info("mq 消费保存积分记录信息 param[{}]", jifen);
                } else if (RocketTag.SAVE_GOODS.equals(e.getTags())) {
                    ChangeReq changeReq = JSONObject.parseObject(e.getBody(), ChangeReq.class);
                    RecycleGoodsService recycleGoodsService = CoreUtils.recycleGoodsService;
                    RecycleGoodsEntity entity = recycleGoodsService.getById(changeReq.getGoodsId());
                    entity.setStatus("已兑换");
                    recycleGoodsService.updateById(entity);
                    log.info("mq 消费商品状态更改信息 param[{}]", entity);
                } else if (RocketTag.SAVE_RECORDLOG.equals(e.getTags())) {
                    ChangeReq changeReq = JSONObject.parseObject(e.getBody(), ChangeReq.class);
                    RecordLogEntity jilu = new RecordLogEntity();
                    RecordLogService recordLogService = CoreUtils.recordLogService;
                    jilu.setRecordUser(changeReq.getUserId());
                    jilu.setGoodsId(changeReq.getGoodsId());
                    recordLogService.save(jilu);
                    log.info("mq 消费兑换记录信息 param[{}]", jilu);
                }
            });
            return ConsumeOrderlyStatus.SUCCESS;
        });
    }
}
